package edu.wong.pro.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.enterprise.inject.New;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.midi.VoiceStatus;
import javax.xml.ws.spi.http.HttpContext;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import edu.wong.pro.iservice.IActivities;

//活动Action
public class ActivitiesAction extends ActionSupport{

	private IActivities iActivities;
	
	
	public IActivities getiActivities() {
		return iActivities;
	}


	public void setiActivities(IActivities iActivities) {
		this.iActivities = iActivities;
	}


	//发布活动
	public String releaseAct(){
	    HttpServletRequest request = (HttpServletRequest) ServletActionContext.getRequest();
	    String actTheme = request.getParameter("actTheme");
	    String actContent = request.getParameter("actContent");
	    String actAddr = request.getParameter("actAddr");
	    String actDate = request.getParameter("actDate");
	    String contacter = request.getParameter("contacter");
	    String phone = request.getParameter("phone");	
	    System.out.println(actTheme+actContent+actAddr+actDate+contacter+phone);
	    iActivities.releaseAct(actTheme, actContent, actDate, actAddr, contacter, phone);
		return "ok";
	}
	
	//获取活动
	public void getAct() throws IOException{
		HttpServletResponse response = (HttpServletResponse) ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();		
		JSONArray array = iActivities.getAct();
		out.print(array);
		out.flush();
		out.close();
	}
	
	//获取最新的前5条活动
		public void getTopAct() throws IOException{
			HttpServletResponse response = (HttpServletResponse) ServletActionContext.getResponse();
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();		
			JSONArray array = iActivities.getTopAct();
			out.print(array);
			out.flush();
			out.close();
		}
		
		//通过ID获取活动
				public void getActByID() throws IOException{
					HttpServletRequest request = (HttpServletRequest) ServletActionContext.getRequest();
					String actId = request.getParameter("actId");
					HttpServletResponse response = (HttpServletResponse) ServletActionContext.getResponse();
					response.setCharacterEncoding("utf-8");
					PrintWriter out = response.getWriter();		
					JSONObject array = iActivities.getActByID(actId);
					out.print(array);
					out.flush();
					out.close();
				}
	
	//参与活动
	public void attend() throws IOException{
		HttpServletRequest request = (HttpServletRequest)ServletActionContext.getRequest();
		String actId = request.getParameter("actId");
		HttpServletResponse response = (HttpServletResponse) ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();		
		boolean ret = iActivities.attends(actId);
		JSONObject jObject = new JSONObject();
	    jObject.put("isattend", ret);
		out.print(jObject);
		out.flush();
		out.close();
	}
	
}
